package com.gemme.gemme.wode;

import android.content.Context;
import android.os.Build;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.gemme.gemme.manager.DataManager;
import com.gemme.gemme.manager.DataPares;
import com.gemme.gemme.view.OnChangeLIstener;

import java.util.HashMap;
import java.util.List;

import androidx.annotation.RequiresApi;

/**
 * @Author: yangcheng
 * @CreateDate: 2020/8/28 18:41
 */
class OrderManager {
    public static final void deleteOrder(Context mContext, long id, OnChangeLIstener onChangeLIstener){
        String url = DataManager.BaseUrl_panghu + "mall/shoporderdetail/"+ id;

        HashMap<String, String> headMap = new HashMap<>();
        headMap.put(DataManager.AuthorizationKey,DataManager.getToken(mContext));
        DataManager.toServer2(mContext, url, DataManager.DELETE_TYPE, "", headMap, new DataManager.GeResponse() {
            @Override
            public void onResponse(String response) {
                try {

                    boolean sucess = DataPares.isSucess(mContext, response);
                    if(sucess){
                        Toast.makeText(mContext, "删除成功", Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(mContext, "删除失败", Toast.LENGTH_SHORT).show();
                    }

                    if(onChangeLIstener != null)
                    onChangeLIstener.onChangeLIstener();

                }catch (Exception e){

                }
                System.out.println("");
            }
        });
    }


    public static final void cancleOrder(Context mContext, long id, OnChangeLIstener onChangeLIstener){
        String url = DataManager.BaseUrl_panghu + "mall/shoporderdetail/closeOrderDetail";

        ShopOrderDetail shopOrderDetail = new ShopOrderDetail();
        if(id < 0){
            Toast.makeText(mContext, "错误不能取消", Toast.LENGTH_SHORT).show();
            return;
        }

        url = url + "/" + id;
//        shopOrderDetail.setId(id + 0L);

//        String jsonString = JSON.toJSONString(shopOrderDetail);

        HashMap<String, String> headMap = new HashMap<>();
        headMap.put(DataManager.AuthorizationKey,DataManager.getToken(mContext));
        DataManager.toServer2(mContext, url, DataManager.GET_TYPE, "", headMap, new DataManager.GeResponse() {
            @RequiresApi(api = Build.VERSION_CODES.O)
            @Override
            public void onResponse(String response) {
                try {
                    boolean sucess = DataPares.isSucess(mContext, response);
                    if(sucess){
                        Toast.makeText(mContext, "取消订单成功", Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(mContext, "取消订单失败", Toast.LENGTH_SHORT).show();
                    }
                    System.out.println("");
                    if(onChangeLIstener != null)
                        onChangeLIstener.onChangeLIstener();
                }catch (Exception e){

                }

            }
        });
    }


    //订单状态修改为确认
    public static final void confirmShouhuoOrder(Context mContext, long id, OnChangeLIstener onChangeLIstener){
        String url = DataManager.BaseUrl_panghu + "mall/shoporderdetail";

        ShopOrderDetail shopOrderDetail = new ShopOrderDetail();
        if(id < 0){
            Toast.makeText(mContext, "错误不能取消", Toast.LENGTH_SHORT).show();
            return;
        }


        shopOrderDetail.setId(id + 0L);
        shopOrderDetail.setOrderStatus(4L);
        String jsonString = JSON.toJSONString(shopOrderDetail);

        HashMap<String, String> headMap = new HashMap<>();
        headMap.put(DataManager.AuthorizationKey,DataManager.getToken(mContext));
        DataManager.toServer2(mContext, url, DataManager.PUT_TYPE, jsonString, headMap, new DataManager.GeResponse() {
            @RequiresApi(api = Build.VERSION_CODES.O)
            @Override
            public void onResponse(String response) {
                try {
                    boolean sucess = DataPares.isSucess(mContext, response);
                    if(sucess){
                        Toast.makeText(mContext, "确认订单成功", Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(mContext, "确认订单失败", Toast.LENGTH_SHORT).show();
                    }
                    System.out.println("");
                    if(onChangeLIstener != null)
                        onChangeLIstener.onChangeLIstener();
                }catch (Exception e){

                }

            }
        });
    }

    //订单状态修改为完成
    public static final void pinglunEndOrder(Context mContext, long id, OnChangeLIstener onChangeLIstener){
        String url = DataManager.BaseUrl_panghu + "mall/shoporderdetail";

        ShopOrderDetail shopOrderDetail = new ShopOrderDetail();
        if(id < 0){
            Toast.makeText(mContext, "错误不能取消", Toast.LENGTH_SHORT).show();
            return;
        }


        shopOrderDetail.setId(id + 0L);
        shopOrderDetail.setOrderStatus(6L);
        String jsonString = JSON.toJSONString(shopOrderDetail);

        HashMap<String, String> headMap = new HashMap<>();
        headMap.put(DataManager.AuthorizationKey,DataManager.getToken(mContext));
        DataManager.toServer2(mContext, url, DataManager.PUT_TYPE, jsonString, headMap, new DataManager.GeResponse() {
            @RequiresApi(api = Build.VERSION_CODES.O)
            @Override
            public void onResponse(String response) {
                try {
                    boolean sucess = DataPares.isSucess(mContext, response);
                    if(sucess){
                        Toast.makeText(mContext, "确认订单成功", Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(mContext, "确认订单失败", Toast.LENGTH_SHORT).show();
                    }
                    System.out.println("");
                    if(onChangeLIstener != null)
                        onChangeLIstener.onChangeLIstener();
                }catch (Exception e){

                }

            }
        });
    }


//    订单状态修改为确认

/*
    public static final void confirmShouhuoOrder(Context mContext, long id, OnChangeLIstener onChangeLIstener){
        String url = DataManager.BaseUrl_panghu + ":9999/mall/shoporderdetail";

        ShopOrderDetail shopOrderDetail = new ShopOrderDetail();
        if(id < 0){
            Toast.makeText(mContext, "错误不能取消", Toast.LENGTH_SHORT).show();
            return;
        }


        shopOrderDetail.setId(id + 0L);
        shopOrderDetail.setOrderStatus(4L);
        String jsonString = JSON.toJSONString(shopOrderDetail);

        HashMap<String, String> headMap = new HashMap<>();
        headMap.put(DataManager.AuthorizationKey,DataManager.getToken(mContext));
        DataManager.toServer2(mContext, url, DataManager.PUT_TYPE, jsonString, headMap, new DataManager.GeResponse() {
            @RequiresApi(api = Build.VERSION_CODES.O)
            @Override
            public void onResponse(String response) {
                try {
                    boolean sucess = DataPares.isSucess(mContext, response);
                    if(sucess){
                        Toast.makeText(mContext, "确认订单成功", Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(mContext, "确认订单失败", Toast.LENGTH_SHORT).show();
                    }
                    System.out.println("");
                    if(onChangeLIstener != null)
                        onChangeLIstener.onChangeLIstener();
                }catch (Exception e){

                }

            }
        });
    }
*/


}
